<script lang="ts" setup>
import { User, EditPen, SwitchButton, CaretBottom } from '@element-plus/icons-vue'
import { useUserInfoStore } from "@/stores/userInfo";
import { useRouter } from "vue-router";
import defaultAvatar from '@/assets/avatar.jpg'
import { ElMessageBox } from 'element-plus';

const userInfoStore = useUserInfoStore();
const router = useRouter();

const handleCommand = (command: string) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
        '你确认要退出吗？',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
    ).then(() => {
          userInfoStore.logout();
        }
    ).catch(() => {
      // 用户取消操作，无需处理
    });

  } else {
    router.push(`/user/${command}`);
  }
}
</script>

<template>
  <el-header>
    <div>
      <!-- 可以留空或显示面包屑导航 -->
    </div>
    <el-dropdown placement="bottom-end" @command="handleCommand">
      <span class="el-dropdown__box">
        <el-avatar :src="userInfoStore.userInfo.avatar || defaultAvatar" />
        <span class="user-info">
          <div class="user-name">{{ userInfoStore.userInfo.name || '未登录' }}</div>
          <div class="user-role">{{ userInfoStore.userInfo.role || '无角色' }}</div>
        </span>
        <el-icon>
          <CaretBottom />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
          <el-dropdown-item command="reset-password" :icon="EditPen">重置密码</el-dropdown-item>
          <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </el-header>
</template>

<style scoped lang="scss">
.el-header {
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .el-dropdown__box {
    display: flex;
    align-items: center;
    cursor: pointer;

    .user-info {
      margin: 0 10px;
      text-align: right;

      .user-name {
        font-weight: bold;
      }
      .user-role {
        font-size: 12px;
        color: #999;
      }
    }

    .el-icon {
      color: #999;
    }

    &:active,
    &:focus {
      outline: none;
    }
  }
}
</style>
